<?php
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : system_power.class.php   FILE_PATH : \\classes\application\system_power.class.php
 * 后台权限操作类
 *
 * @copyright workflow_system 
 * @author 
 * @package 
 * @subpackage 
 * @version Tue Nov 13 17:43:05 CST 2007
 */

defined('Base_Use_Sign') or die("this is invalid system!please contact <a href='http://www.shopnc.net'>www.shopnc.net</>.");

class SystemPowerClass extends FrameWork {
	/**
	 * 构造检索条件
	 * @author workflow Develop Team
	 * @param  
	 * @return str
	 */
	function _conditionSearch($obj_condition){
		$conditionStr = "";

		if ($obj_condition[sys_group_id] != ""){/*权限组ID*/
			$conditionStr .= " and sys_group_id = '" . $obj_condition[sys_group_id] . "'";
		}
		if ($obj_condition['sys_login_name'] != ""){//权限会员名
			$conditionStr .= " and sys_login_name = '" . $obj_condition['sys_login_name'] . "'";
		}
		
		if ($obj_condition[order_group_id] == 1){/*权限组ID*/
			$conditionStr .=" order by sys_group_id";
		}
		if ($obj_condition[sys_member_id] != ""){
			$conditionStr .= " and sys_member_id = '" . $obj_condition[sys_member_id] . "'";
		}
		if ($obj_condition[order_log_id] == 1){/*日志ID*/
			$conditionStr .=" order by sys_log_id desc";
		}
		return $conditionStr;
	}

	/**
	 * 权限组列表
	 * @author workflow Develop Team
	 * @param  $obj_condition 搜索条件
	 * @return array
	 */
	function getSystemGroupList($obj_condition, &$obj_page){
		/**
		 * 构造查询条件
		 */
		$condition_str = $this->_conditionSearch($obj_condition);
		$sys_group_array = $GLOBALS['db']->GetList($condition_str, $obj_page, "system_group");
		return $sys_group_array;
	}

	/**
	 * 新增权限组
	 * @author workflow Develop Team
	 * @param  
	 * @return bool
	 */
	function addSysGroup($input){

		if (!empty($input['sys_group_power'])){
			$line = '|'.@implode('|',$input['sys_group_power']).'|';
		}

		$value_array = array();
		$value_array['sys_group_name']          = $input['sys_group_name'];
//		$value_array['sys_group_area_province'] = $input['txtProvince'];
//		$value_array['sys_group_area_city']     = $input['txtCity'];
		$value_array['sys_group_power']         = $line;
		if ($input['area_id'] != ''){
			$value_array['sys_group_area_id']       = $input['area_id'];
		}
		
		if ($GLOBALS['db']->InsertRow($value_array, 'system_group', 'sys_group_id')) {
			return true;
		}
		return false;
	}

	/**
	 * 获得某ID的权限组信息
	 * @author workflow Develop Team
	 * @param  $sys_group_id 权限组ID
	 * @return array 权限组信息
	 */
	function getSysGroupById($sys_group_id){

		$array = $GLOBALS['db']->GetOneRow($sys_group_id,'system_group','sys_group_id');
		/**
		 * 把权限切割成数组形式
		 */
		if (strstr($array['sys_group_power'],'|')){
			$array['sys_group_power'] = @explode('|',trim($array['sys_group_power'],'|'));
		}
		return $array;
	}

	/**
	 * 更新权限组信息
	 * @author workflow Develop Team
	 * @param  $input 权限组信息参数
	 * @return bool
	 */
	function updateSysGroup($input){

		if (!empty($input['sys_group_power'])){
			$line = '|'.@implode('|',$input['sys_group_power']).'|';
		}
		$value_array = array();
		$value_array['sys_group_name'] = $input['sys_group_name'];
//		$value_array['sys_group_area_province'] = $input['txtProvince'];
//		$value_array['sys_group_area_city']     = $input['txtCity'];
		$value_array['sys_group_power'] = $line;
		if ($input['area_id'] != ''){
			$value_array['sys_group_area_id'] = $input['area_id'];
		}else {
			$value_array['sys_group_area_id'] = 0;
		}
		$update_rs = $GLOBALS['db']->UpdateRow($input['sys_group_id'],$value_array,'system_group','sys_group_id');
		if ($update_rs !== true) {
			return false;
		}
		return true;
	}

	/**
	 * 删除权限组
	 * @author workflow Develop Team
	 * @param  $input 删除的权限组ID数组
	 * @return bool
	 */
	function delSysGroup($input){
		if (intval($input) > 0){
			return $GLOBALS['db']->DelRow($input,'system_group','sys_group_id');
		}else {
			return false;
		}
	}

	
	/**
	 * 转移类别下的信息到另一个类别中
	 * @author workflow Develop Team     
	 * @param  $move_array需要转移的管理员ID数组,$to_id转入的类别ID
	 * @return bool
	 */
	function moveMemberToGroup($move_array,$to_id){
		if (intval($to_id) > 0 && !empty($move_array)){
			foreach ($move_array as $k => $v){
				$condition_str[] = "sys_member_id = '" . $v . "'";
			}
			$condition_str = implode(' or ',$condition_str);
			$value_array = array();
			$value_array['sys_group_id'] = intval($to_id);
			if (substr(trim($condition_str),0,3) == 'and'){
				$condition_str = substr(trim($condition_str),3);
			}
			$result = $GLOBALS['db']->UpdateRows('system_member',$value_array,$condition_str);
			unset($condition,$condition_str,$value_array);
			return $result;
		}
		return false;
	}
	
	/**
	 * 验证管理员登陆
	 * @author workflow Develop Team
	 * @param  $input 登录信息
	 * @return array
	 */
	function checkSysMemberExit($input){
		$array = $GLOBALS['db']->GetList("and sys_login_name='" . $input['sys_login_name'] . "' and sys_password='" . md5($input['sys_password']) . "'",$page,'system_member');
		return $array[0];
	}

	/**
	 * 更新管理登录信息
	 * @author workflow Develop Team
	 * @param  $sys_member_id 管理员ID
	 * @return bool
	 */
	function updateLoginSysMember($sys_member_id){
		if (intval($sys_member_id) > 0){
			//更新次数和登录时间
			$value_array = array();
			$value_array['sys_login_num'] = 'sys_login_num+1';
			$value_array['sys_login_time'] = time();
			$result = $GLOBALS['db']->UpdateRow(intval($sys_member_id),$value_array,'system_member','sys_member_id',true);
			//更新IP
			$value_array = array();
			$value_array['sys_login_ip'] = Common::getIP();
			$result = $GLOBALS['db']->UpdateRow(intval($sys_member_id),$value_array,'system_member','sys_member_id');
			return $result;
		}
	}
/**
	 * 通过ID获得管理员信息
	 * @author workflow Develop Team
	 * @param  $sys_member_id 管理员ID
	 * @return array
	 */
	function getSysMemberById($sys_member_id){
		if (intval($sys_member_id) > 0){
			$array = $GLOBALS['db']->GetOneRow($sys_member_id,'system_member','sys_member_id');
		}
		return $array;
	}
	/**
	 * 添加管理日志
	 * @author workflow Develop Team
	 * @param  $input 日志信息
	 * @return bool
	 */
	function addSysLog($log){
		$value_array = array();
		$value_array['sys_member_id'] = $_SESSION['s_admin']['id'];
		$value_array['sys_login_name'] = $_SESSION['s_admin']['name'];
		$value_array['sys_log_note'] = $log;
		$value_array['sys_log_time'] = time();
		$value_array['sys_log_ip'] = Common::getIP();
		if ($GLOBALS['db']->InsertRow($value_array,'system_log','sys_log_id')) {
			return true;
		}
		return false;
	}

	/**
	 * 管理日志列表
	 * @author workflow Develop Team
	 * @param  
	 * @return array
	 */
	function listSysLog($obj_condition, &$obj_page){
		/**
		 * 构造查询条件
		 */
		$condition_str = $this->_conditionSearch($obj_condition);
		$sys_log_array = $GLOBALS['db']->GetList($condition_str, $obj_page, "system_log");
		return $sys_log_array;
	}

	/**
	 * 安装中，清空权限表和后台管理员表
	 * @author workflow Develop Team
	 * @param  
	 * @return int/bool/object/array
	 */
	function install_system($input){
		$input_param = array();
		$input_param['sys_login_name'] = $input['adminname'];
		$input_param['sys_password'] = $input['password'];
		$input_param['sys_group_id'] = 1;
		$input_param['sys_email'] = $input['email'];
		$this->addSysMember($input_param);
		return true;
	}

	/**
	 * 清空log
	 * @author workflow Develop Team
	 * @param  
	 * @return bool
	 */
	function clean_log(){
		$sql = "TRUNCATE TABLE  {$this->_dbprefix}system_log ";
		if ($GLOBALS['db']->Execute($sql)){
			return true;
		}else{
			$error_report = "You don't clean the log!";
            echo   "{type:0,message:\"$error_report\"}";	
            exit;
		}
	}

}
?>